package com.pactera.madp.cp.controller.oms.marketing;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.marketing.MarketingClassificationCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingClassificationUpdateDTO;
import com.pactera.madp.cp.api.entity.marketing.MarketingClassificationEntity;
import com.pactera.madp.cp.api.vo.marketing.MarketingClassificationVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (McMarketingClassification)表控制层
 *
 * @author yushen.qi
 * @since 2021-05-24 15:30:55
 */

@RestController
@RequestMapping("/marketing-classification")
@Api(value = "marketingClassification", tags = "营销素材-分类管理")
public class MarketingClassificationController {

    @Resource
    private MarketingClassificationService marketingClassificationService;

    /**
     * 新增分类
     *
     * @param dto
     * @return
     */
    @SysLog("新增营销素材分类")
    @PostMapping
    @ApiOperation(value = "新增分类")
    public R<Boolean> save(@RequestBody MarketingClassificationCreateDTO dto){
        MarketingClassificationEntity entity = marketingClassificationService.getOne(Wrappers.<MarketingClassificationEntity>lambdaQuery().eq(MarketingClassificationEntity::getClassificationName, dto.getClassificationName()).eq(MarketingClassificationEntity::getCorpId, CorpContextHolder.getCorpId()));
        if(null!=entity){
            throw new CheckedException("分类名称不能重复！");
        }
        return marketingClassificationService.insert(dto)?R.ok():R.failed("新增失败");
    }

    /**
     * 分类列表
     *
     * @return
     */
    @GetMapping("/index")
    @ApiOperation(value = "分类列表")
    public R<List<MarketingClassificationVO>> list(){
        return R.ok(marketingClassificationService.listAll());
    }

    /**
     * 编辑分类
     *
     * @param dto
     * @return
     */
    @SysLog("编辑营销素材分类")
    @PutMapping
    @ApiOperation(value = "编辑分类")
    public R<Boolean> put(@RequestBody MarketingClassificationUpdateDTO dto){
        MarketingClassificationEntity entity = marketingClassificationService.getOne(Wrappers.<MarketingClassificationEntity>lambdaQuery().eq(MarketingClassificationEntity::getClassificationName, dto.getClassificationName()));
        if (!entity.getId().equals(dto.getId())) {
            throw new CheckedException("分类名称不能重复！");
        }
        return marketingClassificationService.put(dto)?R.ok():R.failed("编辑失败");
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @SysLog("删除营销素材分类")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除分类")
    public R<Boolean> delete(@PathVariable Integer id){
        return marketingClassificationService.removeById(id);
    }

}
